package com.book.common.io;

import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * Description：加载配置文件
 * @Author： leo.xiong
 * @CreateDate： 2019/08/20 18:29
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
public class PropertyLoader implements PropertySourceLoader {

    private static boolean isLoadPropertySource = false;

    @Override
    public String[] getFileExtensions() {
        return new String[]{"properties", "yml"};
    }

    @Override
    public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
        if (!isLoadPropertySource) {
            isLoadPropertySource = true;
            Properties properties = PropertiesUtils.getInstance().getProperties();
            return Collections.singletonList(new OriginTrackedMapPropertySource("book", properties));
        }
        return Collections.emptyList();
    }
}
